<?php
/**
 * 时间服务
 */

namespace common\models\Service;


class TimeService {

    public $found_time = 1435680000; //成立时间 2015-07-01 0:0:0;
    public $now = 0; //当前时间戳
    public $today = 0; //今天
    public $yesterday = 0; //昨天
    public $this_week = 0; //本周
    public $this_month = 0; //本月
    public $last_month = array(0, 0); //上个月
    public $last_week = array(0, 0); //上周
    public $last_7days = 0; //最近7日
    public $last_30days = 0; //最近30日

    private $start_time;
    private $hr; //当前几点了


    function __construct()
    {
        $this->now = time();

        $this->today = strtotime('today');
        $this->yesterday = strtotime('yesterday');

        $this->hr = date('H', $this->now);

        $this->this_week = strtotime('last Monday');
        $this->this_month = strtotime(date('Y-m-1 0:0:0', $this->now));

        $this->last_week = [
            strtotime('-2 Monday'),
            strtotime('-1 Sunday'),
        ];
        $this->last_week = [
            strtotime(date('Y-m-d 0:0:0', strtotime('first day of last month '))),
            strtotime(date('Y-m-d 0:0:0', strtotime('last day of last month '))),
        ];

        $this->last_7days = strtotime('-7 day');
        $this->last_30days = strtotime('-30 day');


    }

    /*
     * 按小时获取今天的时间戳
     */
    public function getTodayByHour(){
        $hours = array();
        for($i=0; $i<=$this->hr; $i++){
            $hours[] = $this->today + $i*3600;
        }
        return $hours;
    }

    /*
     * 获取最近24小时
     */
    public function getLast24h(){
        $hours = array();
        for($i=$this->hr-24; $i<=$this->hr; $i++){
            $hours[] = $this->today + $i*3600;
        }
        return $hours;
    }

    /*
     * 获取最近30天时间戳
     */
    public function getLastSomeDays($num){
        $days = array();
        for($i = $num; $i >= 0; $i--){
            $days[] = $this->today - $i * 3600 * 24;
        }
        return $days;
    }

    /*
     * 计时器开始
     */
    public function startTimer(){
        $this->start_time = microtime(true);
    }

    /*
     * 计时器结束并返回时间差
     */
    public function endTimer(){
        return microtime(true) - $this->start_time;
    }

    /*
     * 获得某天的24小时数组
     * @param string $date 格式如图：20150701, 默认为当天
     * @return data ['title', 'data']
     */
    public static function init24Hours($date = null){
        $now = time();
        if(empty($date)){
            $start = strtotime(date('Y-m-d', $now));
        }else{
            $start = strtotime($date);
        }

        $output = ['title'=>[], 'data'=>[]];
        for($i = 0; $i<24; $i++){
            $output['data'][$i] = $start + 3600 * $i;
            if($output['data'][$i] > $now)
                $output['data'][$i] = 0;
            $output['title'][$i] = $i.'点';
        }

        return $output;
    }

    /**
     * 初始化最近30天时间戳数组
     * @param int|string $start_date 某天之前的30天
     * @param int|string $end_date
     * @return array 时间戳数组
     */
    public static function initSomeDays($start_date = 30, $end_date = 0)
    {
        $TimeService = new TimeService();
        $output = [];

        //把零散时间戳格式化为整天
        if($start_date > 100){
            $start_date = strtotime(date('Y-m-d 0:0:0', $start_date));
        }
        if($end_date == 0) {
            $end_date = $TimeService->today;
        }else{
            $end_date = strtotime(date('Y-m-d 0:0:0', $end_date));
        }

        //计算天数
        if($start_date <= 100){
            $days = $start_date;
        }else{
            $days = ($end_date - $start_date)/(3600*24);
        }

        for($i = $days; $i>=0; $i--){

            $suffix = '日';
            $date = $end_date - $i * 24 * 3600;
            $j = intval(date('j', $date));
            $n = intval(date('n', $date));
            if($i == $start_date || ($n === 1 && $j === 1)){
                $output['title'][] = date('Y年n月j', $date) . $suffix;
            }elseif($n !== 1 && $j === 1){
                $output['title'][] = date('n月j', $date) . $suffix;
            }else{
                $output['title'][] = $j . $suffix;
            }

            $output['data'][]  = $date;
        }

        return $output;
    }


    /*
     * 按天初始化某个月时间戳数组
     * @param string month 月份 比如
     * @return bool|array
     */
    public function initOneMonthByDay($month = null){
        if(empty($month) || strtolower($month) == 'now'){
            $start = strtotime(date('Y-m-01 0:0:0', $this->now));
        }else{
            if(strlen($month) == 6){
                $start = strtotime($month.'01');
            }else{
                return false;
            }
        }
        $days = date('t', $start);
        $output = array();
        for($i = 0; $i < $days; $i++){
            $output[$i] = $start + 3600 * 24 * $i;
        }
        return $output;
    }

    /**
     * 产生最近N个月的数组
     * @param int $months 月数，默认为12个月
     * @return array [
     *     'title' => ['2015.8', '9', '10'],
     *     'data' => [],
     * ]
     */
    public function initRecentYearByMonthArray($months = 12){
        $now = time();
        //将 年.月 的形式转换成月数，比如：2015年9月 => 24189
        $months_found = date('Y', $this->found_time)*12 + date('n', $this->found_time);
        $months_now   = date('Y', $now) * 12 + date('n', $now);
        if(($months_now - $months_found) < $months){
            $months_start = $months_found;
        }else{
            $months_start = $months_now - 12;
        }
        $output = [
            'title' => [],//标题
            'data' => [],//时间戳
        ];
        for($i = $months_start; $i<=$months_now+1; $i++){
            $y = floor($i/12);
            $m = $i%12;
            if($m == 0){
                $m = 12;
                $y -= 1;
            }
            if($i == $months_start || $m === 1){
                $output['title'][] = $y.'年'.$m.'月';
            }else{
                $output['title'][] = $m.'月';
            }
            $output['data'][] = strtotime($y.'-'.$m.'-1 0:0:0');
        }
        array_pop($output['title']);
        return $output;




    }


    public static function alltotime($input)
    {
        if(preg_match('/^\d{11}$/', $input)){
            return $input;
        }else{
            return strtotime($input);
        }
    }
}